//
// Created by Jisam on 2024/8/11 19:29.
// solve d
#include <bits/stdc++.h>

using namespace std;
struct node {
    int x, y, z;
};
int dx[] = {-1, 1, 0, 0,};
int dy[] = {0, 0, -1, 1,};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    vector<vector<int>> a(n, vector(n, 0)), vis(n, vector(n, 0));
    vector<list<int>> vi, vj;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> a[i][j];
            if (a[i][j] == 1) {
                vi[i].push_back(i);
                vj[j].push_back(j);
            }
        }
    }
    queue<node> q;
    q.push({0, 0, 0});
    while (!q.empty()) {
        auto [x, y, z] = q.front();

        if (x == n - 1 && y == n - 1) {
            cout << z;
            return 0;
        }
        for (int i = 0; i < n; i++) {
            int tx = x + dx[i];
            int ty = y + dy[i];
            int f = 0;
            if (tx < 0) {
                f = 1;
                tx += 2;
                while (tx != n - 1 && a[tx][ty] != 1) {
                    tx++;
                }
                tx++;
            }
            if (ty < 0) {
                f = 2;
                ty += 2;
                while (ty != n - 1 && a[tx][ty] != 1) {
                    ty++;
                }
                ty++;
            }
            if (tx > n) {
                f = 3;
                tx -= 2;
                while (tx != 0 && a[tx][ty] != 1) {
                    tx--;
                }
                tx--;
            }
            if (ty > n) {
                ty -= 2;
                while (ty != n - 1 && a[tx][ty] != 1) {
                    ty--;
                }
                ty--;
                f = 4;
            }
            if (a[tx][ty]) {
                q.push({tx, ty, z});
                vis[tx][ty] = 1;
            } else {
                q.push({tx, ty, z + 1});
                vis[tx][ty] = 1;
            }

        }
    }
    cout << "-1";
    return 0;
}